package com.todo.biz.application.handler.bizData;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.todo.biz.application.entity.context.SimpleBusinessDataContext;
import com.todo.biz.application.remote.ContentInfoRemoteService;
import com.todo.biz.application.remote.UserInfoRemoteService;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.content.api.request.QueryContentInfoByIdListRequest;
import com.todo.content.api.response.ContentInfoResponse;
import com.todo.user.api.response.BatchQuerySimplyUserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/5/30 11:12
 * @description
 */
@Slf4j
@Component
public class ContentDataHandler extends AbstractBusinessDataHandle {

    @Resource
    private UserInfoRemoteService userInfoRemoteService;
    @Resource
    private ContentInfoRemoteService contentInfoRemoteService;


    @Override
    protected Map<Long, SimpleBusinessDataContext> doQueryBizData(List<Long> bizIdList) {
        if (CollectionUtils.isEmpty(bizIdList)) {
            return Maps.newHashMap();
        }
        QueryContentInfoByIdListRequest request = new QueryContentInfoByIdListRequest();
        request.setContentIdList(bizIdList);
        BizResponse<List<ContentInfoResponse>> listBizResponse = contentInfoRemoteService.queryContentInfoByIdList(request);
        if (listBizResponse == null || !listBizResponse.isSuccess() || CollectionUtils.isEmpty(listBizResponse.getData())) {
            log.warn("ContentDataHandler#doQueryBizData 根据主键查询内容详情完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Maps.newHashMap();
        }
        List<ContentInfoResponse> contentBaseInfoResponses = listBizResponse.getData();
        List<Long> userIdList = contentBaseInfoResponses.stream().map(ContentInfoResponse::getUserId).collect(Collectors.toList());
        BizResponse<BatchQuerySimplyUserInfoResponse> response = userInfoRemoteService.batchQuerySimplyUserInfo(userIdList);
        if (response == null || !response.isSuccess() || response.getData() == null || MapUtils.isEmpty(response.getData().getSimplyUserInfoMap())) {
            log.warn("ContentDataHandler#doQueryBizData 根据主键查询用户信息完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Collections.emptyMap();
        }
        Map<Long, BatchQuerySimplyUserInfoResponse.SimplyUserInfo> simplyUserInfoMap = response.getData().getSimplyUserInfoMap();

        Map<Long, SimpleBusinessDataContext> resultMap = Maps.newHashMap();
        for (ContentInfoResponse contentBaseInfoResponse : contentBaseInfoResponses) {
            if (contentBaseInfoResponse == null || !simplyUserInfoMap.containsKey(contentBaseInfoResponse.getUserId())) {
                continue;
            }

            SimpleBusinessDataContext context = new SimpleBusinessDataContext();
            context.setBizId(String.valueOf(contentBaseInfoResponse.getContentId()));

            BatchQuerySimplyUserInfoResponse.SimplyUserInfo simplyUserInfo = simplyUserInfoMap.get(contentBaseInfoResponse.getUserId());
            SimpleBusinessDataContext.UserInfo userInfo = new SimpleBusinessDataContext.UserInfo();
            userInfo.setUserId(String.valueOf(contentBaseInfoResponse.getUserId()));
            userInfo.setUserNikeName(simplyUserInfo.getUserNikeName());
            userInfo.setUserAvatar(simplyUserInfo.getUserAvatar());
            userInfo.setLocation(simplyUserInfo.getLocation());
            context.setUserInfo(userInfo);
            context.setTitle(contentBaseInfoResponse.getTitle());
            context.setMainImg(contentBaseInfoResponse.getMainImg());
            context.setSummary(contentBaseInfoResponse.getContent() == null ? "" : (contentBaseInfoResponse.getContent().length() > 100 ? contentBaseInfoResponse.getContent().substring(0, 99) : contentBaseInfoResponse.getContent()));
            context.setLikeCount(contentBaseInfoResponse.getLikeCount());
            context.setCommentCount(contentBaseInfoResponse.getCommentCount());
            context.setCollectCount(contentBaseInfoResponse.getCollectCount());
            context.setBusinessScene(BusinessSceneEnum.CONTENT);
            resultMap.put(contentBaseInfoResponse.getContentId(), context);

        }
        return resultMap;
    }

    @Override
    public BusinessSceneEnum getHandleBusinessScene() {
        return BusinessSceneEnum.CONTENT;
    }
}
